//
//  MyCollectionCell.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/8/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyCollectionCell.h"

//#import "WeatherCell.h"

#import "HederView.h"
#import "MyButton.h"
//#import "ZSCell.h"
//#import "QSCell.h"
#import "URL.h"
#import "ZSButton.h"
#import "QSButton.h"
#import "UIImageView+WebCache.h"
//#import "WeatherDownCell.h"

@interface MyCollectionCell ()
@property (nonatomic, retain)CAShapeLayer*lineLayer2;
@property (nonatomic, retain)CAShapeLayer*lineLayer;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic,assign)CGFloat a;
@property (nonatomic,assign)CGFloat b;

@end

@implementation MyCollectionCell
-(void)dealloc
{
    
    [_pointView release];
    [_tableV release];
    [_viewT release];
    [_yesterButton release];
    [_todayButton release];
    [_tomButton release];
    [_houtianButton release];
    [_dahoutainButton release];
    [_lastButton release];
    [_upImage release];
    [_upLable release];
    [_downImage release];
    [_downLable release];
    
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
   
    
    self = [super initWithFrame:frame];
    if (self) {

       
        self.QSArr = [NSMutableArray array];
        self.weakArr = [NSMutableArray array];
     
        self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 49 * OffHeight) style:UITableViewStylePlain];
        self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
        self.tableV.delegate = self;
        self.tableV.dataSource = self;
        self.tableV.backgroundColor = [UIColor clearColor];
        self.tableV.showsVerticalScrollIndicator = NO;
        self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.contentView addSubview:_tableV];
    
#warning dfsfdsafdsafsdafdsafsdafsd
        
        self.viewT = [[HederView alloc]initWithFrame:CGRectMake(0, 0, 0, self.frame.size.height - 49 * OffHeight)];
        self.viewT.userInteractionEnabled = YES;
        self.viewT.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.1];
        self.tableV.tableHeaderView = _viewT;
        
       
        self.pointView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 200 * OffHeight)];
        
        
//        if (self.QSArr != nil) {
//            [self.tableV reloadData];
//            self.tableV.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 49 * OffHeight);
//        }
   

        self.yesterButton = [[QSButton alloc]initWithFrame:CGRectMake(0, 0, (self.viewT.frame.size.width - 6 * OffWidth) / 6, 300 * OffHeight)];
        self.yesterButton.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.7];
        [_yesterButton release];
        
        self.todayButton = [[QSButton alloc]initWithFrame:CGRectMake(self.viewT.frame.size.width / 6 , 0, (self.viewT.frame.size.width - 6 * OffWidth) / 6, 300 * OffHeight)];
        self.todayButton.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.6];

        [_todayButton release];
        self.tomButton = [[QSButton alloc]initWithFrame:CGRectMake(self.viewT.frame.size.width / 6 * 2, 0, (self.viewT.frame.size.width - 6 * OffWidth) / 6, 300 * OffHeight)];
        self.tomButton.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.5];

        [_tomButton release];
        self.houtianButton = [[QSButton alloc]initWithFrame:CGRectMake(self.viewT.frame.size.width / 6 * 3, 0, (self.viewT.frame.size.width - 6 * OffWidth) / 6, 300 * OffHeight)];
        self.houtianButton.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.4];

        [_houtianButton release];
        self.dahoutainButton = [[QSButton alloc]initWithFrame:CGRectMake(self.viewT.frame.size.width / 6 * 4, 0, (self.viewT.frame.size.width - 6 * OffWidth) / 6, 300 * OffHeight)];
        self.dahoutainButton.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.3];

        [_dahoutainButton release];
        self.lastButton = [[QSButton alloc]initWithFrame:CGRectMake(self.viewT.frame.size.width / 6 * 5 , 0, (self.viewT.frame.size.width - 6 * OffWidth) / 6, 300 * OffHeight)];
        self.lastButton.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.2];

        [_lastButton release];
        self.upImage = [[UIImageView alloc]initWithFrame:CGRectMake(5 * OffWidth, 5 * OffHeight, self.frame.size.width - 10 * OffWidth, 130 * OffHeight)];
        [_upImage release];
        self.upLable = [[UILabel alloc]initWithFrame:CGRectMake(5 * OffWidth, 105 * OffHeight, self.frame.size.width - 10 * OffWidth, 30 * OffHeight)];
        [_upLable release];
        
        self.downImage = [[UIImageView alloc]initWithFrame:CGRectMake(10 * OffWidth, 10 * OffHeight, 100* OffWidth, 80 * OffHeight)];
        [_downImage release];
        
        self.downLable = [[UILabel alloc]initWithFrame:CGRectMake(130 * OffWidth, 5, self.frame.size.width - 110 * OffWidth, 30 * OffHeight)];
        [_downLable release];
       
        self.downLableFrom = [[UILabel alloc]initWithFrame:CGRectMake(130 * OffWidth, 65, self.frame.size.width - 110 * OffWidth, 30 * OffHeight)];
        self.downLableFrom.font = [UIFont boldSystemFontOfSize:15];
        
       self.downLableTime = [[UILabel alloc]initWithFrame:CGRectMake(self.contentView.frame.size.width - 50 * OffWidth, 70, self.frame.size.width - 100 * OffWidth, 30 * OffHeight)];
        self.downLableTime.font = [UIFont boldSystemFontOfSize:13];
    }
    return self;
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
}

- (void)setQSName:(NSString *)QSName
{
    if (_QSName != QSName) {
        _QSName = QSName;
//        self.tableV.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 49 * OffHeight);
        self.tableV.contentOffset = CGPointMake(0, 0);
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
   
    if (indexPath.section == 0){
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QS"];
        if (cell == nil) {
            
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"QS"];
//              if (self.type == 0) {
            
         
        [cell addSubview:_yesterButton];
    
        [cell addSubview:_todayButton];
        
        [cell addSubview:_tomButton];
        
        [cell addSubview:_houtianButton];
                
        [cell addSubview:_dahoutainButton];
                
        [cell addSubview:_lastButton];
        
            
        }
        self.c = 0;
         if (self.QSArr.count != 0) {
          
        
             [self setValueWithButton:self.yesterButton temp:0];
             [self setValueWithButton:self.todayButton temp:1];
             [self setValueWithButton:self.tomButton temp:2];
             [self setValueWithButton:self.houtianButton temp:3];
             [self setValueWithButton:self.dahoutainButton temp:4];
             [self setValueWithButton:self.lastButton temp:5];
      
             
             [self drawLinepoint1y:self.yesterButton.frame.size.height - self.yesterButton.high.text.intValue * 7   point2y:self.yesterButton.frame.size.height - self.todayButton.high.text.intValue * 7  point3y:self.yesterButton.frame.size.height - self.tomButton.high.text.intValue * 7 point4y:self.yesterButton.frame.size.height - self.houtianButton.high.text.intValue * 7  point5y:self.yesterButton.frame.size.height - self.dahoutainButton.high.text.intValue * 7 point6y:self.yesterButton.frame.size.height - self.lastButton.high.text.intValue * 7];
             
             self.c = 1;
             
             [self drawAgainLinepoint1y: self.yesterButton.frame.size.height -self.yesterButton.low.text.intValue * 7  point2y:self.yesterButton.frame.size.height - self.todayButton.low.text.intValue * 7  point3y:self.yesterButton.frame.size.height - self.tomButton.low.text.intValue * 7 point4y: self.yesterButton.frame.size.height - self.houtianButton.low.text.intValue * 7 point5y:self.yesterButton.frame.size.height - self.dahoutainButton.low.text.intValue * 7 point6y:self.yesterButton.frame.size.height - self.lastButton.low.text.intValue * 7];
           
           
         

          
//            self.type++;
        
             
             [cell addSubview:self.pointView];
//            }
        
        
            
        }
        
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
        
    }else if (indexPath.section == 1) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZS"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ZS"];
        }
        
        
        NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"穿衣指数",@"汽车指数",@"感冒指数",@"紫外线指数",@"旅游指数",@"运动指数", nil];
        NSMutableArray *arrImage = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"yifu.png"],[UIImage imageNamed:@"qiche.png"], [UIImage imageNamed:@"ganmao"],[UIImage imageNamed:@"ziwaixian"],[UIImage imageNamed:@"lvyou.png"],[UIImage imageNamed:@"zixingche.png"],nil];
        
        NSArray *cellArr = cell.subviews;
        for (UIView *view in cellArr) {
            [view removeFromSuperview];
            
        }
        
        
        
        for (int i = 0; i < 6; i ++) {
            
            ZSButton *button = [ZSButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(i % 2 * (self.contentView.frame.size.width - 4 * OffWidth )/ 2 + 2, i  / 2 * (210 * OffHeight - 5) / 3  + 2 * OffHeight, (self.contentView.frame.size.width - 8 * OffWidth )/ 2, (210 * OffHeight - 10 * OffHeight) / 3);
            button.title.text = [arr objectAtIndex:i];
            button.imageV.image = [arrImage objectAtIndex:i];
            button.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.5];

            button.tag = i + 1;
            [button addTarget:self action:@selector(zsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:button];
            
            
        }
   
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
        
    }else{
    

        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
                
            }
            
            _upLable.textColor = [UIColor whiteColor];
            _upLable.font = [UIFont boldSystemFontOfSize:15];
            _upLable.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
            [cell addSubview:_upImage];
            
            
            [cell addSubview:_upLable];
            cell.backgroundColor = [UIColor clearColor];
            return cell;

        }else{
            
            UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"se"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"se"];
                
            }
            
            _downLable.textColor = [UIColor whiteColor];
            _downLable.font = [UIFont boldSystemFontOfSize:15];
            _downLable.numberOfLines = 0;
            [cell addSubview:_downImage];
           [cell addSubview:_downLable];
            _downLableFrom.textColor = [UIColor grayColor];
            _downLableFrom.font = [UIFont boldSystemFontOfSize:15];
            [cell addSubview:_downLableFrom];
            _downLableTime.textColor = [UIColor whiteColor];
            
            [cell addSubview:_downLableTime];
         
            cell.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
            return cell;
        }
        
     
    }
    
    
    

}


//封装趋势 button  并赋值
- (void)setValueWithButton:(QSButton *)button temp:(NSInteger )temp
{
 
    if (temp == 1) {
        button.weak.textColor = [UIColor greenColor];
        button.highPoint.textColor = [UIColor greenColor];
        button.lowPoint.textColor = [UIColor greenColor];
        button.high.textColor = [UIColor greenColor];
        button.low.textColor = [UIColor greenColor];
        button.wind.textColor = [UIColor greenColor];
        button.sky.textColor = [UIColor greenColor];
    }
    button.weak.text = [[self .QSArr  objectAtIndex:temp] objectForKey:@"dateName"];
    button.sky.text = [[[self.QSArr objectAtIndex:temp] objectForKey:@"climate"] objectForKey:@"name"];
    button.wind.text = [[[self.QSArr objectAtIndex:temp] objectForKey:@"winds"] objectForKey:@"direction"];
    
    button.high.text = [[[[self.QSArr objectAtIndex:temp] objectForKey:@"temp"] objectForKey:@"height"] stringValue];
    button.low.text = [[[[self.QSArr objectAtIndex:temp] objectForKey:@"temp"] objectForKey:@"low"] stringValue];
    
    button.high.frame = CGRectMake(20 * OffWidth,270 * OffHeight - button.high.text.intValue * 7, 30 * OffWidth, 30 * OffHeight);
    button.low.frame = CGRectMake(20 * OffWidth, 300 * OffHeight - button.low.text.intValue * 7  , 30 * OffWidth, 30 * OffHeight);
    button.highPoint.frame = CGRectMake(38 * OffWidth,260 * OffHeight - button.high.text.intValue * 7, 30 * OffWidth, 30 * OffHeight);
    button.highPoint.text = @"。";
    button.lowPoint.frame = CGRectMake(38 * OffWidth, 290 * OffHeight - button.low.text.intValue * 7  , 30 * OffWidth, 30 * OffHeight);
    button.lowPoint.text = @"。";
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.section == 0) {
        
         [self.xieyi gotoAnimationsWithArr:self.QSArr name:self.QSName];
    }else if (indexPath.section == 2){
        
         [tableView deselectRowAtIndexPath:indexPath animated:YES];
         [self.xieyi gotoWebViewWithURL:indexPath.row];
    }
    
 
}



#warning 画线
- (void)drawAgainLinepoint1y:(CGFloat)point1y point2y:(CGFloat)point2y point3y:(CGFloat)point3y point4y:(CGFloat)point4y point5y:(CGFloat)point5y point6y:(CGFloat)point6y
{
    
    [self.lineLayer2 removeFromSuperlayer];
    //第一、UIBezierPath绘制线段
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //四个点  ? 明明是六个点啊????
    CGPoint point = CGPointMake((self.viewT.frame.size.width / 12),  point1y);
    CGPoint point1 = CGPointMake(self.viewT.frame.size.width / 6 + (self.viewT.frame.size.width / 12), point2y);
    CGPoint point2 = CGPointMake(self.viewT.frame.size.width / 6 * 2 + (self.viewT.frame.size.width / 12), point3y);
    CGPoint point3 = CGPointMake(self.viewT.frame.size.width / 6 * 3+ (self.viewT.frame.size.width / 12), point4y);
    CGPoint point4 = CGPointMake(self.viewT.frame.size.width / 6 * 4 + (self.viewT.frame.size.width / 12), point5y);
    CGPoint point5 = CGPointMake(self.viewT.frame.size.width / 6  * 5+ (self.viewT.frame.size.width / 12), point6y);
    
#warning 将结构体转化为对象类型放入数组中
    NSArray *arr = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:point],[NSValue valueWithCGPoint:point1],[NSValue valueWithCGPoint:point2],[NSValue valueWithCGPoint:point3],[NSValue valueWithCGPoint:point4],[NSValue valueWithCGPoint:point5], nil];
    
#warning 定义索引集合, 索引集合的特征: 索引值唯一, 有序, 无符号整数
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, arr.count)];
    
    // 相当于遍历数组 - ()
    [arr enumerateObjectsAtIndexes:set options:0 usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop){
        
        CGPoint point = [pointValue CGPointValue];
        [path addLineToPoint:point];
        
        //（一）rect折线画法
        CGRect rect;
        rect.origin.x = point.x - 2.5 * OffWidth;
        rect.origin.y = point.y - 2.5 * OffHeight;
        rect.size.width = 5 * OffWidth;
        rect.size.height = 5 * OffHeight;
        
        //（二）rect射线画法
        //        CGRect rect = CGRectMake(10, 10, 1, 1);
        
        UIBezierPath *arc= [UIBezierPath bezierPathWithOvalInRect:rect];
        [path appendPath:arc];
    }];
    
    //第三、UIBezierPath和CAShapeLayer关联
    self.lineLayer2 = [CAShapeLayer layer];
    
    
    
    _lineLayer2.frame = CGRectMake(0, 0, 320 * OffWidth, 400 * OffHeight);
    _lineLayer2.fillColor = [UIColor redColor].CGColor;
    _lineLayer2.path = path.CGPath;
    _lineLayer2.strokeColor = [UIColor redColor].CGColor;
    
    [self.pointView.layer addSublayer:_lineLayer2];
    
}

#warning 画线方法
- (void)drawLinepoint1y:(CGFloat)point1y point2y:(CGFloat)point2y point3y:(CGFloat)point3y point4y:(CGFloat)point4y point5y:(CGFloat)point5y point6y:(CGFloat)point6y
{
    
    [self.lineLayer removeFromSuperlayer];

    //第一、UIBezierPath绘制线段
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //六个点
    CGPoint point = CGPointMake((self.viewT.frame.size.width / 12),  point1y);
    CGPoint point1 = CGPointMake(self.viewT.frame.size.width / 6 + (self.viewT.frame.size.width / 12), point2y);
    CGPoint point2 = CGPointMake(self.viewT.frame.size.width / 6 * 2 + (self.viewT.frame.size.width / 12), point3y);
    CGPoint point3 = CGPointMake(self.viewT.frame.size.width / 6 * 3+ (self.viewT.frame.size.width / 12), point4y);
    CGPoint point4 = CGPointMake(self.viewT.frame.size.width / 6 * 4 + (self.viewT.frame.size.width / 12), point5y);
    CGPoint point5 = CGPointMake(self.viewT.frame.size.width / 6  * 5+ (self.viewT.frame.size.width / 12), point6y);
    
    
    NSArray *arr = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:point],[NSValue valueWithCGPoint:point1],[NSValue valueWithCGPoint:point2],[NSValue valueWithCGPoint:point3],[NSValue valueWithCGPoint:point4],[NSValue valueWithCGPoint:point5], nil];
    
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, arr.count)];
    
    // 对数组进行遍历
    [arr enumerateObjectsAtIndexes:set options:0 usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop){
        
        CGPoint point = [pointValue CGPointValue];
        [path addLineToPoint:point];
        
        //（一）rect折线画法
        CGRect rect;
        rect.origin.x = point.x - 2.5 * OffWidth;
        rect.origin.y = point.y - 2.5 * OffHeight;
        rect.size.width = 5 * OffWidth;
        rect.size.height = 5 * OffHeight;
        
        //（二）rect射线画法
        //        CGRect rect = CGRectMake(10, 10, 1, 1);
        
        UIBezierPath *arc= [UIBezierPath bezierPathWithOvalInRect:rect];
        [path appendPath:arc];
    }];
    //第三、UIBezierPath和CAShapeLayer关联
    self.lineLayer = [CAShapeLayer layer];

    
    _lineLayer.frame = CGRectMake(0, 0, 320 * OffWidth, 400 * OffHeight);
    _lineLayer.fillColor = [UIColor redColor].CGColor;
    _lineLayer.path = path.CGPath;
    _lineLayer.strokeColor = [UIColor redColor].CGColor;

    [self.pointView.layer addSublayer:_lineLayer];
   
}

- (void)zsButtonClick:(UIButton *)button
{
    
    [self.xieyi send:button];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 2;
    }else{
    return 1;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        return 300 * OffHeight;
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
           return 135 * OffHeight;
        }else{
            return 100 * OffHeight;
        }
        
    }else{
        return 210 * OffHeight;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return  [self sectionWithLable:@"趋势"];
    }else if(section == 1) {
        return [self sectionWithLable:@"指数"];
    }else{
        return [self sectionWithLable:@"生活"];
    }
}

- (UIView *)sectionWithLable:(NSString *)label
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor =  [[UIColor blackColor] colorWithAlphaComponent:0.4];
    
    
    UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(50 * OffWidth, 24 * OffHeight, 310 * OffWidth, 0.5 * OffHeight)];
    line.backgroundColor = [UIColor grayColor];
    [view addSubview:line];
    [line release];
   
    
    UILabel * lineName = [[UILabel alloc]initWithFrame:CGRectMake(10 * OffWidth, 0, 50 * OffWidth, 50 * OffHeight)];
    lineName.text = label;
    lineName.font = [UIFont boldSystemFontOfSize:15 * OffWidth];
    lineName.textColor = [UIColor whiteColor];
    [view addSubview:lineName];
    [lineName release];
    [view autorelease];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50 * OffHeight;
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (scrollView == self.tableV) {
//        [self.xieyi sendTableViewContentSize:self.tableV.contentOffset.y];
//        
//    }
//}
@end
